<?php

global $connect;
$query = "INSERT INTO commandes (nmagasin, datelivre, livrer,centrale) VALUES (".$_POST['idmag'].",".$_POST['date'].", 0, 1);";
mysql_query($query, $connect);
$query = "SELECT ncommande AS 'NCOMMANDE' FROM commandes WHERE nmagasin = ".$_POST['idmag']." AND livrer = 0 ORDER BY datecmd DESC LIMIT 1;";
$res = mysql_query($query, $connect) or die(mysql_error());
$idcmd = mysql_fetch_array($res);

$query = "INSERT INTO choisir (ncommande, nfournisseur) VALUES (".$idcmd['NCOMMANDE'].", '".$_POST['fournisseur']."');";
$res = mysql_query($query, $connect) or die(mysql_error());


foreach ($_POST as $key=>$v)
{
	if ($key == 'fournisseur' || $key == 'date' || $key == 'idmag')
		{
			$i = 0;
		}
	else
	{
		$req = "INSERT INTO contenir (nproduit, nmagasin, ncommande, quantitecmd) VALUES (".$key.",".$_POST['idmag'].",".$idcmd['NCOMMANDE'].",".$v.");";
		mysql_query($req, $connect) or die(mysql_error());
	}
}
echo "<div class='box_success'>La commande est valid&eacute;e</div><br/>";

echo "<center><h1><a href='index.php?page=magasins&show_mag_id=".$_POST['idmag']."'> retour sur les produits</a></h1></center>";
?>